import CesiumApp from '../CesiumApp';
import VGE from '../../../VGE';

class DrawRectHandler {
	constructor(dom, scene, iCesium) {
		this.startPoint = null;
		this.dom = dom;
		this.scene = scene;
		this.iCesium = iCesium;
		this.handler = new iCesium.ScreenSpaceEventHandler(scene.canvas);
		this.rect = new Rect(dom, iCesium);
		this.isDestroyed = false;
		this.activeEvt = new iCesium.Event();
		this.cancelEvt = new iCesium.Event();
		this.movingEvt = new iCesium.Event();
		this.drawEvt = new iCesium.Event();
	}

	// 开关相机控制
	setCameraCtrl(isActive) {
		const cameraCtrl = this.scene.screenSpaceCameraController;
		cameraCtrl.enableRotate = isActive;
		cameraCtrl.enableTranslate = isActive;
		cameraCtrl.enableZoom = isActive;
		cameraCtrl.enableTilt = isActive;
		cameraCtrl.enableLook = isActive;
	}

	// 鼠标cursor控制
	toggleCursorStyle(isActive) {
		if (isActive) {
			CesiumApp.viewer.enableCursorStyle = false;
			CesiumApp.viewer._element.style.cursor = '';
			this.dom.style.cursor = 'default';
		} else {
			CesiumApp.viewer.enableCursorStyle = true;
		}
	}

	// 初始化事件处理
	initEvent(self) {
		this.handler.setInputAction(function (e) {
			// isShiftAndLeftDown = true
			self.startPoint = new self.iCesium.Cartesian2(e.position.x, e.position.y);
			self.addListener(self);
		}, this.iCesium.ScreenSpaceEventType.LEFT_DOWN);
	}

	addListener(self) {
		this.handler.setInputAction(function (e) {
			const endPosition = e.endPosition;
			self.activeEvt.raiseEvent(endPosition);
			// if (!isShiftAndLeftDown) return
			self.rect.setPosition(self.startPoint, endPosition);
			self.rect.setVisible(true);
			self.movingEvt.raiseEvent(self.rect.getRectPoint());
		}, this.iCesium.ScreenSpaceEventType.MOUSE_MOVE);
		this.handler.setInputAction(function (e) {
			self.rect.setVisible(false);
			self.drawEvt.raiseEvent(self.rect.getRectPoint());
			self.handler.removeInputAction(self.iCesium.ScreenSpaceEventType.MOUSE_MOVE);
			self.handler.removeInputAction(self.iCesium.ScreenSpaceEventType.LEFT_UP);
		}, this.iCesium.ScreenSpaceEventType.LEFT_UP);
	}

	// 移除事件监听
	removeEvent() {
		this.handler.removeInputAction(this.iCesium.ScreenSpaceEventType.LEFT_DOWN);
		this.handler.removeInputAction(this.iCesium.ScreenSpaceEventType.MOUSE_MOVE);
		this.handler.removeInputAction(this.iCesium.ScreenSpaceEventType.LEFT_UP);
		this.rect.setVisible(false);
	}

	destroy() {
		if (this.isDestroyed) return;
		this.setCameraCtrl(true);
		this.removeEvent();
		this.handler.destroy();
		this.rect.destroy();
		this.rect = null;
		this.isDestroyed = true;
	}

	activate() {
		if (this.isDestroyed) return;
		this.setCameraCtrl(false);
		this.toggleCursorStyle(true);
		this.initEvent(this);
	}

	deactivate() {
		if (this.isDestroyed) return;
		this.setCameraCtrl(true);
		this.toggleCursorStyle(false);
		this.removeEvent();
		for (const layer of CesiumApp.viewer.scene.layers.layerQueue) {
			layer.selectColorType = 2;
			layer.removeAllObjsColor();
		}
	}
}

// div框
class Rect {
	constructor(parentDom, iCesium) {
		this.rect = document.createElement('div');
		this.dom = parentDom;
		this.iCesium = iCesium;
		this.rect.style.visibility = 'hidden';
		this.leftTopPoint = new iCesium.Cartesian2();
		this.rightBottomPoint = new iCesium.Cartesian2();
		this.dom.appendChild(this.rect);
	}

	setPosition(startPoint, endPosition) {
		const w = endPosition.x - startPoint.x;
		const h = endPosition.y - startPoint.y;
		let left, top, width, height;
		if (w < 0) {
			left = endPosition.x;
			width = -w;
		} else {
			left = startPoint.x;
			width = w;
		}
		if (h < 0) {
			top = endPosition.y;
			height = -h;
		} else {
			top = startPoint.y;
			height = h;
		}
		this.leftTopPoint = new this.iCesium.Cartesian2(left, top);
		this.rightBottomPoint = new this.iCesium.Cartesian2(left + width, top + height);
		this.rect.style = `position:absolute;top:${top}px;left:${left}px;width:${width}px;height:${height}px;border:2px dashed #FFF;`;
		this.setVisible(true);
	}

	setVisible(isVisible) {
		this.rect.style.visibility = isVisible ? 'visible' : 'hidden';
	}

	getRectPoint() {
		return {
			leftTopPoint: this.leftTopPoint,
			rightBottomPoint: this.rightBottomPoint
		};
	}

	destroy() {
		this.dom.removeChild(this.rect);
		this.rect = null;
	}
}

export function onBoxSelection(viewer, iCesium) {
	const scene = viewer.scene;
	const dom = viewer.container;
	// 挂载框选监听事件
	const drawRect = (window.drawRect = new DrawRectHandler(dom, scene, iCesium));
	const selectedObjs = Object.create(null, {});
	const resetIds = Object.create(null, {});
	// 初始设置各个图层的选择值为空
	for (const layer of scene.layers.layerQueue) {
		selectedObjs[layer.name] = [];
		resetIds[layer.name] = [];
	}
	// res为框的左上点与右下点的相对于给定dom节点的坐标
	drawRect.drawEvt.addEventListener(function (res) {
		for (const layer of scene.layers.layerQueue) {
			// layer.selectColorType = 1
			layer.selectColorType = 3;
		}
		const selectedColor = new iCesium.Color(VGE.config.primaryColor);
		scene.pickRect(res.leftTopPoint, res.rightBottomPoint);
		const objs = scene.getPickRectIDs();
		for (const k in selectedObjs) {
			if (Object.prototype.hasOwnProperty.call(selectedObjs, k)) {
				scene.layers.find(k).removeObjsColor(selectedObjs[k]);
			}
		}
		for (const layer of scene.layers.layerQueue) {
			const layerName = layer.name;
			const layerSlt = objs.find((e) => e.layerName === layerName);
			const sltIds = (layerSlt && layerSlt.ids) || [];
			resetIds[layerName] = selectedObjs[layerName];
			selectedObjs[layerName] = sltIds;

			layer.setObjsColor(selectedObjs[layerName], selectedColor);
		}
	});

	drawRect.movingEvt.addEventListener(function (res) {
		for (const layer of scene.layers.layerQueue) {
			// layer.selectColorType = 1
			layer.selectColorType = 2;
		}
		scene.pickRect(res.leftTopPoint, res.rightBottomPoint);
	});

	const canvas = document.querySelector('canvas');
	canvas.setAttribute('tabindex', '0'); // needed to put focus on the canvas
	canvas.focus();
}
